<template>
  <div>
    <!-- //水厂管理 -->
    <div class="factory-wrapper">
      <el-row>
        <el-col :span="24" class="common-top-viscous-style">
          <el-row>
            <el-col :span="12">
              <el-input
                v-model="filterName"
                class="search"
                size="small"
                placeholder="请输入检索内容"
                suffix-icon="el-icon-search"
                clearable
                @change="getWorks"
                @clear="getWorks"
              />
            </el-col>
            <el-col :span="12" align="right">
              <el-button type="primary" size="small" @click="addWaterFn">添加水厂</el-button>
            </el-col>
          </el-row>
        </el-col>
        <el-col :span="24">
          <el-card>
            <el-table :data="factData.list" size="small">
              <el-table-column
                v-for="item in factColumns"
                :key="item.key"
                :prop="item.key"
                :label="item.title"
              />
              <el-table-column fixed="right" label="操作" width="150">
                <template slot-scope="scope">
                  <el-button type="text" size="small" @click="checkStructureManage(scope.row)">构筑物</el-button>
                  <el-button type="text" size="small" @click="editWaterFn(scope.row)">编辑</el-button>
                  <el-button type="text" size="small" @click="delWorksFn(scope.row)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <el-row>
              <el-col :span="24" align="center" class="common-pagination">
                <pagination
                  v-show="total>0"
                  :total="total"
                  :page.sync="queryParams.pageNum"
                  :limit.sync="queryParams.pageSize"
                  @pagination="getList"
                />
              </el-col>
            </el-row>
          </el-card>
        </el-col>
      </el-row>
    </div>
    <factorySave ref="waterMap" :save-obj="saveObj" @getWorks="getWorks" />
  </div>
</template>

<script>
import { getWorks, delWorks } from '@/api/infoConfig/factoryManage'
import factorySave from './component/save'
export default {
  components: {
    factorySave
  },
  data() {
    return {
      total: 0,
      queryParams: {
        pageNum: 1,
        pageSize: 10
      },
      filterName: '',
      factData: {
        list: []
      },
      factColumns: [
        { key: 'enterpriseUid', title: '企业ID', width: 180 },
        {
          key: 'worksCode',
          title: '水厂编号',
          width: 180
        },
        {
          key: 'worksName',
          title: '水厂名称',
          width: 180
        },
        {
          key: 'stdName',
          title: '水质标准',
          width: 180
        },
        {
          key: 'worksAddress',
          title: '水厂地址',
          width: 180
        },
        {
          key: 'worksLng',
          title: '经度',
          width: 180
        },
        {
          key: 'worksLat',
          title: '纬度',
          width: 180
        },
        {
          key: 'worksDesc',
          title: '水厂描述',
          width: 180
        },
        {
          key: 'userName',
          title: '水厂厂长',
          width: 180
        },
        {
          key: 'craftUserName',
          title: '工艺主管',
          width: 180
        },
        {
          key: 'equUserName',
          title: '设备主管',
          width: 180
        }
      ],
      saveObj: {
        show: false,
        title: '',
        data: {
          worksUid: '',
          enterpriseUid: '',
          worksCode: '',
          worksName: '',
          stdUid: '',
          worksAddress: '',
          worksLng: '',
          worksLat: '',
          worksDesc: '',
          userUid: '',
          craftUserName: '',
          equUserName: '',
          sort: ''
        },
        opts: []
      }
    }
  },
  created() {
    this.getWorks()
  },
  methods: {
    getList(data) {
      // console.log(data)
      this.getWorks()
    },
    // 获取全部水厂
    getWorks() {
      getWorks({
        worksName: this.filterName,
        page_size: this.queryParams.pageSize,
        current_page: this.queryParams.pageNum
      }).then(res => {
        console.log(res)
        this.total = res.data.total
        this.factData.list = res.data.list
      })
    },
    addWaterFn() {
      this.saveObj.title = '添加水厂'
      this.saveObj.data = {
        worksUid: '',
        enterpriseUid: '',
        worksCode: '',
        worksName: '',
        stdUid: '',
        worksAddress: '',
        worksLng: '',
        worksLat: '',
        worksDesc: '',
        userUid: '',
        craftUser: '',
        equUser: ''
      }
      this.$refs.waterMap.getFormFn()
      this.$refs.waterMap.checkEdit(false)
    },
    editWaterFn(row) {
      // console.log(row)
      this.saveObj.title = '编辑水厂'
      this.saveObj.data = JSON.parse(JSON.stringify(row))
      this.$refs.waterMap.getFormFn()
      this.$refs.waterMap.checkEdit(true)
    },
    delWorksFn(row) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          delWorks({ uid: row.worksUid }).then(res => {
            console.log(res)
            if (res.msg === '请求成功') {
              this.getWorks()
            }
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 构筑物
    checkStructureManage() {}
  }
}
</script>
<style lang="scss">
@import "./factoryManage.scss";
</style>
